﻿Imports System.Windows.Forms
Public Class frmMain
    ''' <summary>
    ''' 转换类
    ''' </summary>
    ''' <remarks></remarks>
    Private Converter As New ConvertControl
    ''' <summary>
    ''' PL1输入文件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdPL1Source_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPL1Source.Click
        Using mOpenFileDialog As New OpenFileDialog
            If mOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtPL1Source.Text = mOpenFileDialog.FileName
                txtCOBOLSource.Text = mOpenFileDialog.FileName.Replace("PL1", "COBOL")
            End If
        End Using
    End Sub
    ''' <summary>
    ''' COBOL输入文件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdCOBOLSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCOBOLSource.Click
        Using mSaveFileDialog As New SaveFileDialog
            If mSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtCOBOLSource.Text = mSaveFileDialog.FileName
            End If
        End Using
    End Sub
    ''' <summary>
    ''' 开始转换
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRun.Click
        Try
            Dim mFileInfoList As New clsFileInfoList
            mFileInfoList = ReadObjectFromXMLFile(GetType(clsFileInfoList), MyConfig.FileInfoFileName)
            mFileInfoDic.Clear()
            For Each mFile As CommonMethod.FileInfoRec In mFileInfoList.FileInfo
                mFileInfoDic.Add(mFile.ProgID & "|" & mFile.FileID, mFile)
            Next
            Dim mCopyBookInfoList As New clsCopyBookList
            mCopyBookInfoList = ReadObjectFromXMLFile(GetType(clsCopyBookList), MyConfig.CopyInfoFileName)
            mCopyBookInfoDic.Clear()
            For Each mCopyBook As CommonMethod.CopyBookInfoRec In mCopyBookInfoList.CopyBookInfo
                mCopyBookInfoDic.Add(mCopyBook.CopyBookName, mCopyBook)
            Next
        Catch ex As Exception
            If MsgBox("无法读取文件情报列表，是否要继续转换?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then Exit Sub
        End Try
        '捕获完成进度
        AddHandler Converter.StepCompleted, Sub(StrMessage)
                                                Me.Text = "PL1转换工具-" & StrMessage
                                            End Sub
        Dim ConverResult As ConversionResult
        ConverResult = Converter.ConvertFile(txtPL1Source.Text, txtCOBOLSource.Text)
        Select Case ConverResult
            Case ConversionResult.OK
                MsgBox("转换成功")
            Case ConversionResult.FileNotFound
                MsgBox("文件没有找到")
            Case ConversionResult.Exception
                MsgBox("异常终了")
                Diagnostics.Process.Start(MyConfig.ErrorReportName)
        End Select
    End Sub
    ''' <summary>
    ''' 测试用
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '打开方法测试窗体
        AddHandler cmdTest.Click, Sub()
                                      Using mTest As New frmTest
                                          mTest.ShowDialog()
                                      End Using
                                  End Sub
        '选项窗体
        AddHandler cmdOption.Click, Sub()
                                        Using mOption As New frmOption
                                            mOption.ShowDialog()
                                        End Using
                                    End Sub
        Try
            MyConfig = IOSystemBase.ReadObjectFromXMLFile(GetType(clsConfig), Application.StartupPath & "\Config.xml")
        Catch ex As Exception
            MyConfig = New clsConfig
            MyConfig.Init()
            IOSystemBase.WriteObjectToXMLFile(MyConfig, GetType(clsConfig), Application.StartupPath & "\Config.xml")
        End Try
        If TestMode Then
            txtPL1Source.Text = "C:\ConversionTool\TestData\PL1\Demo.txt"
            txtCOBOLSource.Text = "C:\ConversionTool\TestData\COBOL\Demo.txt"
        End If
    End Sub
End Class
